package cn.ang.app.controller;

import cn.ang.app.pojo.DevUser;
import cn.ang.app.service.DevUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/developer")
public class DeveloperController {

    @Resource(name = "devUserServiceImpl")
    private DevUserService devUserService;

    @RequestMapping("/do/{page}")
    public String doBackendLogin(@PathVariable String page) {
        return page;
    }

    @RequestMapping("/login")
    public @ResponseBody boolean devLogin(DevUser devUser, HttpSession session) {
        DevUser devUserLogin = devUserService.devLogin(devUser);
        session.setAttribute("devUserLogin",devUserLogin);
        return devUserLogin != null ? true : false;
    }

    /**
     * 当前登录的用户信息
     * @param session 从会话作用域中取出信息
     * @return ajax返回到客户端
     */
    @RequestMapping("/sessionDevUser")
    public @ResponseBody DevUser viewDevUser(HttpSession session) {
        DevUser sessionDevUser = (DevUser) session.getAttribute("devUserLogin");
        return sessionDevUser;
    }

    @RequestMapping("/loginOut")
    public @ResponseBody boolean loginOut( HttpSession session ) {
        Object devUserLogin = session.getAttribute("devUserLogin");
        if( devUserLogin != null ) {
            session.removeAttribute("devUserLogin");
            return true;
        } else {
            return false;
        }
    }

}
